package com.itboye.shop.me.qianbao;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.shop.R;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.YuEPresenter;
import com.itboye.shop.util.helper.utils.OnPasswordInputFinish;
import com.itboye.shop.util.helper.utils.PasswordView;
import com.itboye.shop.util.loadutil.Const;
import com.itboye.shop.util.loadutil.IsUtilUid;
import com.itboye.shop.util.loadutil.MAlert;
import com.itboye.shop.util.loadutil.SPUtils;

import java.util.Observable;
import java.util.Observer;

/**
 * 设置支付密码（完成设置）
 */
public class ZhiFuPassSheZhiTwoActivity extends BaseActivity implements Observer{
    TextView tvTitle;
    ImageView back;

    PasswordView pwdView;// 支付密码
    String NewsPwd;
    Intent intent;
    String pass;

    YuEPresenter yuEPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhi_fu_pass_she_zhi_two);
        tvTitle.setText("设置支付密码");

        yuEPresenter=new YuEPresenter(this);

        intent = this.getIntent();
        if (intent != null) {
            pass = intent.getStringExtra("pass");
        }

        final PasswordView pwdView = (PasswordView) findViewById(R.id.pwd_view);

        // 添加密码输入完成的响应
        pwdView.setOnFinishInput(new OnPasswordInputFinish() {
            @Override
            public void inputFinish() {
                // 输入完成后跳转
                // 也就是说——>实现你的交易逻辑什么的在这里写
                NewsPwd=pwdView.getStrPassword();
                if(NewsPwd.equals(pass)){
                    showProgressDialog("设置密码中，请稍后...",true);
                    yuEPresenter.OnSetUpZhiFuPass(IsUtilUid.isUid(),"",NewsPwd);

                }else{
                    MAlert.alert("两次密码不一致", Gravity.CENTER);
                }
            }
        });

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable o, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == YuEPresenter.zhifu_pass_success) {
                MAlert.alert(entity.getData(),Gravity.CENTER);
                SPUtils.put(ZhiFuPassSheZhiTwoActivity.this, null, Const.PaySecret, pass);
                finish();
            }
            if (entity.getEventType() == YuEPresenter.zhifu_pass_fail) {
                MAlert.alert(entity.getData(),Gravity.CENTER);
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
